package com.lhl.jwt.security.configs;

import com.lhl.jwt.handler.JwtAuthenticationFailureHandler;
import com.lhl.jwt.security.filters.WxAuthenticationFilter;
import io.swagger.annotations.Api;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.HttpSecurityBuilder;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.logout.LogoutFilter;

/**
 * @className: com.lhl.jwt.security.configs.WxAppletConfigurer
 * @description: 微信配置文件
 * @author: king
 * @date: 2020-12-22 17:34
 **/
@Api(tags = "微信配置文件", produces = "WxConfigurer")
public class WxConfigurer<T extends WxConfigurer<T, B>, B extends HttpSecurityBuilder<B>>
        extends AbstractHttpConfigurer<T, B> {

    private WxAuthenticationFilter wxAuthenticationFilter;

    public WxConfigurer() {
        wxAuthenticationFilter = new WxAuthenticationFilter();
    }

    @Override
    public void configure(B builder) throws Exception {
        wxAuthenticationFilter.setAuthenticationManager(builder.getSharedObject(AuthenticationManager.class));

        wxAuthenticationFilter = postProcess(wxAuthenticationFilter);
        builder.addFilterAfter(wxAuthenticationFilter, LogoutFilter.class);
    }

    public WxConfigurer<T, B> authenticationSuccessHandler(AuthenticationSuccessHandler successHandler) {
        wxAuthenticationFilter.setAuthenticationSuccessHandler(successHandler);
        return this;
    }

    public WxConfigurer<T, B> authenticationFailureHandler(JwtAuthenticationFailureHandler jwtAuthenticationFailureHandler) {
        wxAuthenticationFilter.setAuthenticationFailureHandler(jwtAuthenticationFailureHandler);
        return this;
    }
}
